package org.cn.osiris.pet.controller;

import io.swagger.annotations.ApiOperation;
import org.cn.osiris.org.domain.Department;
import org.cn.osiris.pet.service.IPetTypeService;
import org.cn.osiris.pet.domain.PetType;
import org.cn.osiris.pet.query.PetTypeQuery;
import org.cn.osiris.basic.util.AjaxResult;
import org.cn.osiris.basic.util.PageList;
import org.cn.osiris.system.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/petType")
public class PetTypeController {

    @Autowired
    public IPetTypeService petTypeService;

    /**
     * 接口：添加或修改
     * @param petType  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    @PreAuthorize(name = "添加或修改",sn = "PetType:save")
    public AjaxResult addOrUpdate(@RequestBody PetType petType){
        try {
            if( petType.getId()!=null) {
                petTypeService.update(petType);
            } else {
                petTypeService.add(petType);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "删除",sn = "PetType:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            petTypeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    @PreAuthorize(name = "批量删除",sn = "PetType:patchDel")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
                petTypeService.batchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    @PreAuthorize(name = "查询单个",sn = "PetType:getOne")
    public PetType findById(@PathVariable("id")Long id) {
        return petTypeService.findById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    @PreAuthorize(name = "查询所有",sn = "PetType:getAll")
    public List<PetType> findAll(){
        return petTypeService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @PreAuthorize(name = "分页",sn = "PetType:list")
    public PageList<PetType> queryPage(@RequestBody PetTypeQuery query) {
        return petTypeService.queryPage(query);
    }


    @ApiOperation(value = "无限级树")
    @GetMapping("/tree")
    @PreAuthorize(name = "无限级树",sn = "PetType:tree")
    public List<PetType> queryTree(){
        System.out.println("=======================================");
        List<PetType> petTypes = petTypeService.queryTree();
        System.out.println("=======================================");
        return petTypes;
    }


}
